﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;


namespace SPHelper
{
	public class Publishing
	{
		///// <summary>
		///// Pages folder for passed web
		///// </summary>
		///// <param name="web">If null, will get pages for root website</param>
		///// <returns></returns>
		//public static SPFolder GetPagesRootFolder(SPWeb web)
		//{
		//    SPFolder pages = null;
		//    try
		//    {
		//        // init
		//        if (web == null) { web = Web.GetWeb(); }
		//        if (web == null) { throw new Exception("SPWeb passed is null or can't get from SPContext"); }

		//        pages = web.GetFolder("Pages");
		//    }
		//    catch (Exception ex)
		//    {
		//        Log.WriteError("75uiejrhwgwesd", ex);
		//        pages = null;
		//    }
		//    return pages;
		//}

		

		/// <summary>
		/// Get PublishingSite from passed SPSite
		/// </summary>
		/// <param name="site"></param>
		/// <returns></returns>
		public static PublishingSite GetPublishingSite(SPSite site)
		{
			PublishingSite pubSite = null;
			try 
			{	        
				if (site == null) { site = Site.GetSite(); }
				if (site == null) { throw new Exception("SPSite from SPContext is not a Publishing Site"); }
				if (!PublishingSite.IsPublishingSite(site)) { throw new Exception("SPSite passed is not a publishing site"); }
				pubSite = new PublishingSite(site);
			}
			catch (Exception ex)
			{
				Log.WriteError("tdyuy6ertsdsdssxse", ex);
				pubSite = null;
			}
			return pubSite;
		}

		public static PublishingSite GetPublishingSite()
		{
			return GetPublishingSite(null);
		}

		public static PublishingWeb GetPublishingWeb(SPWeb web)
		{
			PublishingWeb pubWeb = null;
			try 
			{	        
				if (web == null) { web = Web.GetWeb(); }
				if (web == null) { throw new Exception("SPWeb passed is null or can't get from SPContext"); }
				if (!PublishingWeb.IsPublishingWeb(web)) { throw new Exception("SPWeb passed is not a publishing web: " + web.Title); }
				pubWeb = PublishingWeb.GetPublishingWeb(web);
			}
			catch (Exception ex)
			{
				Log.WriteError("sretytukdfdfrd", ex);
				pubWeb = null;
			}
			return pubWeb;
		}

		public static PublishingWeb GetPublishingWeb()
		{
			return GetPublishingWeb(null);
		}




		/// <summary>
		/// set default page layout for an SPWeb
		/// </summary>
		/// <param name="pageLayout"></param>
		/// <param name="pubWeb"></param>
		/// <param name="isApplyToAllChildWebs"></param>
		public static void SetDefaultPageLayout(PageLayout pageLayout, PublishingWeb pubWeb, bool isApplyToAllChildWebs)
		{
			try
			{
				// init
				if (pageLayout == null) { throw new Exception("Page Layout passed is null"); }
				if (pubWeb == null)
				{
					pubWeb = Publishing.GetPublishingWeb();
					if (pubWeb == null) { throw new Exception("Failed to pass publishingWeb and SPContext is null"); }
				}
				pubWeb.SetDefaultPageLayout(pageLayout, isApplyToAllChildWebs);
			}
			catch (Exception ex)
			{
				Log.WriteError("78897ryesdfsraeaer4", ex);
			}
		}

		/// <summary>
		/// Set default page layout for passes publishing web
		/// </summary>
		/// <param name="pageLayoutFilename"></param>
		/// <param name="pubWeb"></param>
		/// <param name="isApplyToAllChildWebs"></param>
		public static void SetDefaultPageLayout(string pageLayoutFilename, PublishingWeb pubWeb, PublishingSite pubSite, bool isApplyToAllChildWebs)
		{
			try
			{
				// init
				if (string.IsNullOrEmpty(pageLayoutFilename)) { throw new Exception("Page Layout filename passed is null"); }
				if (pubSite == null) { pubSite = Publishing.GetPublishingSite(); }
				if (pubSite == null) { throw new Exception("PublishingSite passed is null and failed to get from SPContext"); }


				// get page layout
				PageLayout pageLayout = Page.GetPageLayout(pubSite, pageLayoutFilename);
				if (pageLayout == null) { throw new Exception("Failed to get page layout using filename: " + pageLayoutFilename); }

				// call original
				SetDefaultPageLayout(pageLayout, pubWeb, isApplyToAllChildWebs);
			}
			catch (Exception ex)
			{
				Log.WriteError("8oufygfdgdrfdttrd", ex);
			}
		}
		
	}
}
